import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:family_app/src/features/secure_vault/models/vault_item_model.dart';
import 'package:family_app/src/theme/app_theme.dart';

/// 私密信息项列表项组件
class VaultItemListTile extends StatelessWidget {
  final VaultItemModel item;
  final VoidCallback onTap;
  final VoidCallback onFavoriteToggle;
  final VoidCallback onDelete;

  const VaultItemListTile({
    Key? key,
    required this.item,
    required this.onTap,
    required this.onFavoriteToggle,
    required this.onDelete,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Card(
      elevation: 1,
      margin: EdgeInsets.symmetric(horizontal: 16.w, vertical: 4.h),
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12.r)),
      child: ListTile(
        onTap: onTap,
        contentPadding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 8.h),
        leading: _buildLeadingIcon(),
        title: Text(
          item.title,
          style: TextStyle(fontWeight: FontWeight.bold, fontSize: 16.sp),
        ),
        subtitle: Text(
          item.username,
          style: TextStyle(color: AppTheme.textSecondaryColor, fontSize: 14.sp),
        ),
        trailing: Row(
          mainAxisSize: MainAxisSize.min,
          children: [
            IconButton(
              icon: Icon(
                item.isFavorite ? Icons.star : Icons.star_border,
                color: item.isFavorite ? Colors.amber : Colors.grey,
              ),
              onPressed: onFavoriteToggle,
            ),
            IconButton(
              icon: const Icon(Icons.delete_outline, color: Colors.grey),
              onPressed: onDelete,
            ),
          ],
        ),
      ),
    );
  }

  /// 构建前导图标
  Widget _buildLeadingIcon() {
    Color backgroundColor;
    IconData iconData;

    // 根据类型设置背景色和图标
    switch (item.type) {
      case VaultItemType.website:
        backgroundColor = Colors.blue[50]!;
        iconData = Icons.language;
        break;
      case VaultItemType.app:
        backgroundColor = Colors.green[50]!;
        iconData = Icons.apps;
        break;
      case VaultItemType.bankCard:
        backgroundColor = Colors.orange[50]!;
        iconData = Icons.credit_card;
        break;
      case VaultItemType.idCard:
        backgroundColor = Colors.purple[50]!;
        iconData = Icons.badge;
        break;
      case VaultItemType.other:
        backgroundColor = Colors.grey[50]!;
        iconData = Icons.more_horiz;
        break;
    }

    return Container(
      width: 40.r,
      height: 40.r,
      decoration: BoxDecoration(
        color: backgroundColor,
        borderRadius: BorderRadius.circular(8.r),
      ),
      child: Icon(
        iconData,
        color: AppTheme.primaryColor,
        size: 24.r,
      ),
    );
  }
}
